package com.ecy.quadratic.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.ecy.quadratic.R;
import com.ecy.quadratic.beans.user.User;
import com.ecy.quadratic.manager.LoginManager;
import com.ecy.quadratic.reader.base.BaseActivity;
import com.ecy.quadratic.reader.component.AppComponent;
import com.ecy.quadratic.reader.utils.ToastUtils;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    private UMAuthListener mUmAuthListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
            String temp = "";
            User user = new User();

            for (String key : map.keySet()) {
                temp += key + ":" + map.get(key) + "\n";
                if (key.equals("uid")) {
                    user.thirdpartyId = map.get(key);
                }
                if (key.equals("name")) {
                    user.nickname = map.get(key);
                }
                if (key.equals("gender")) {
                    user.gender = map.get(key);
                }
                if (key.equals("iconurl")) {
                    user.portrait = map.get(key);
                }
                if (key.equals("age")) {
                    user.age = map.get(key);
                }
            }
            System.out.println(temp);
            ToastUtils.getSingleToast("登录成功", Toast.LENGTH_SHORT).show();
            LoginManager.getInstance().saveUser(user);
            finish();
        }

        @Override
        public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

        }

        @Override
        public void onCancel(SHARE_MEDIA share_media, int i) {

        }
    };

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    public void initToolBar() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void configViews() {
        hideStatusBar();
    }

    @OnClick(R.id.iv_qq_login)
    public void qqLogin() {
        UMShareAPI.get(LoginActivity.this).getPlatformInfo(LoginActivity.this, SHARE_MEDIA.QQ, mUmAuthListener);
    }

    @OnClick(R.id.iv_wechat_login)
    public void wechatLogin() {
        UMShareAPI.get(LoginActivity.this).getPlatformInfo(LoginActivity.this, SHARE_MEDIA.WEIXIN, mUmAuthListener);
    }

    @OnClick(R.id.iv_weibo_login)
    public void weboLogin() {
        UMShareAPI.get(LoginActivity.this).getPlatformInfo(LoginActivity.this, SHARE_MEDIA.SINA, mUmAuthListener);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }
}
